-- Decompiled with the Synapse X Luau decompiler.

--单例 永久存在

local ClientOHOH = _G.ClientOHOH;
local FunctionList = ClientOHOH.load.FunctionList();
FunctionList.create_character = true;
local event_manager = ClientOHOH.event_manager();

local CommonOHOH =_G.CommonOHOH
local ConnectClass=CommonOHOH.load.Connect();
local training_cfg = ClientOHOH.config.Training
local game_cfg = ClientOHOH.config.Game
local LanguageHelper=_G.LanguageHelper 
local Client_AudioManager = ClientOHOH.load.Client_AudioManager();
local M = ClientOHOH.class.new(script, ClientOHOH.files.Client_PlayerObject());
local TeleportService = game:GetService("TeleportService")
local FirendListHelper=_G.CommonOHOH.load.FirendListHelper()

function M.new(p1)
	return M(p1);
end;

function M.ctor(self, player)
	_G.CommonOHOH.load.PlayerTool().CreateConnectEvent(self)
	self.isInBattle=false -- 是否在战斗中
	M.super.ctor(self, player);

	self.DataBase =self:GetComponent(ClientOHOH.load.Client_PlayerDataComponent())

	self.oldLevel=self.DataBase:GetData("base").level

	
	self.BattleSateEvent:Connect(function(isEnter)
		self.isInBattle=isEnter
	end)


	-- 设置相机远近
	self:SetCameraMinZoomDistance(game_cfg.CameraMinZoomDistance)
	self:SetCameraMaxZoomDistance(game_cfg.CameraMaxZoomDistance)

	local isCanHangUp=true --是否能够无限挂机
	for _,userId in pairs(game_cfg.HangUpBlacklist) do
		if userId==game.Players.LocalPlayer.UserId then
			isCanHangUp=false
			break
		end
	end
	
	if isCanHangUp then
		--超时跳转
		self.main_inst.Idled:Connect(function(time)
			if time>=game_cfg.MaxIdledTime then
				self:IdledOutTimeTeleport()
			end
		end)
	end
end;

-- 获取是否在战斗状态
function M.IsInBattle(self)
	return self.isInBattle
end

--开始跳转
function M.IdledOutTimeTeleport(self)

	local workValue=0
	if self:IsAutoAttack() then
		workValue=2--攻击
	elseif self:IsAutoTrain() then
		workValue=1--训练
	end

	local teleportOptions = Instance.new("TeleportOptions")
	local teleportData = {
		placeId = game.PlaceId,
		jobId = game.JobId,
		work=workValue
	}
	teleportOptions:SetTeleportData(teleportData)

	local attemptIndex = 0
    local success, result -- define pcall results outside of loop so results can be reported later on

    repeat
        success, result = pcall(function()
			return 	TeleportService:Teleport(game.PlaceId,game.Players.LocalPlayer,teleportOptions)
        end)
        attemptIndex += 1
        if not success then
            task.wait(1)
        end

    until success or attemptIndex == 5 -- stop trying to teleport if call was successful, or if retry limit has been reached

    if not success then
        warn(result) -- print the failure reason to output
    end
end

function M.PlayerDead(self)
	self.DeadBeforeEvent:Action()
	self.character:SetHp(0)
end

function M.dtor(self)

	if self.UserDataEvent then
		self.UserDataEvent:dtor()
		self.UserDataEvent=nil
	end

	M.super.dtor(self);
end;

-- 收服务器的数据同步
function M.SynchDataFormeServer(self,data)
	assert(self.DataBase,"self.DataBase==null")
	for key,value in pairs(data) do
		self.DataBase:UpdateData(key,value)
	end

	self.UserDataEvent:Action(data)
end


--已经有数据了（构建的player实例（ctor）的时候，就会调用）
function M.OnLogin(self)


	self:AddComponent(_G.CommonOHOH.load.Common_GamePassCtrl()); 

end;

--通知服务器登录完成后调用客户端登录完成 （构建的player实例（ctor）的时候，就会调用）
function M.OnLoginOver(self)

	self:AddComponent(ClientOHOH.load.Client_SynShootBullet()); 
	self:AddComponent(ClientOHOH.load.Client_BeamLineCtrl()); 
	self:AddComponent(ClientOHOH.load.Client_BgmComponent()); 
	self:AddComponent(ClientOHOH.load.Client_HomeCtrl()); 
	self:AddComponent(ClientOHOH.load.Client_UpdateVersonCtrl());
	self:AddComponent(ClientOHOH.load.Client_SkyComponent());
	-- self:AddComponent(ClientOHOH.load.Client_ActivityTeleport());

	
	_G.CommonOHOH.load.PlayerTool().AddCommonComponent(self)


	local s,d= pcall(function()
		game:GetService("StarterGui"):SetCore("ResetButtonCallback", false);
		game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.Health, false);
	end)


	self:AddPrivateComponentByFolder(script.Private)

end;



function M.CreateCharacter(self)
	self:Invoke(FunctionList.create_character);
end;

function M.OnCharacterIn(self, Character)
	self.CharacterEnterEvent:Action(Character)

	local teleportData = TeleportService:GetLocalPlayerTeleportData()
	if teleportData then
		local s, work = xpcall(function()
			return teleportData:GetTeleportData().work
		end, debug.traceback)
		if s then
			if work==2 then--自动攻击
				self:AddProcess(function()
					self:Wait(10)
					if not self:IsAutoAttack() then
						self:ChangeAutoAttackState()
					end
				end)
			else
				self:AddProcess(function()
					self:Wait(10)
					if not self:IsAutoTrain() then
						self:ChangeAutoTrainState()
					end
				end)

			end
		else
			self:AddProcess(function()
				self:Wait(10)
				if not self:IsAutoTrain() then
					self:ChangeAutoTrainState()
				end
			end)
		end
	else
	end
	
end;

function M.OnCharacterOut(self, p12)

end;

--销毁进行游戏的组件
function M.DestroyGameComponent(self)
	
end;



function M.IsAutoAttack(self)
	return self.AutoAttacking
end
function M.IsAutoTrain(self)
	return self.AutoTraining
end

function M.ChangeAutoTrainState(self)

	if self.AutoTraining then
		self.AutoTraining=false
	else
		self.AutoTraining=true
	
		if self.AutoAttacking then --关闭自动攻击
			self.AutoAttacking=false
			self.AutoAttckEvent:Action(self.AutoAttacking)
		end
		
	end
	self.AutoTrainEvent:Action(self.AutoTraining)
end

function M.ChangeAutoAttackState(self)
	if self.AutoAttacking then
		self.AutoAttacking=false
	else
		self.AutoAttacking=true
		if self.AutoTraining then --关闭自动训练
			self.AutoTraining=false
			self.AutoTrainEvent:Action(self.AutoTraining)
		end
	end
	self.AutoAttckEvent:Action(self.AutoAttacking)
end


function M.StopAutoAttackState(self)

	-- print("自动攻击中----》修改为不自动攻击",self:IsAutoAttack())

	if self:IsAutoAttack() then
		-- print("自动攻击中----》修改为不自动攻击")
		self:ChangeAutoAttackState()
	end
end




--服务器通知客户端创建游戏对象控制器
event_manager:RegistHandle(ClientOHOH.event_list.server_GameObject_Control, function(player, gameObject, GameObjectControlClass)
	print("服务器说 创建游戏对象 控制器:",gameObject,GameObjectControlClass)
	ClientOHOH.load[GameObjectControlClass]().new(gameObject)
end);

event_manager:RegistHandle(ClientOHOH.event_list.synch_userdata, function(player,data)
	player:SynchDataFormeServer(data)

end);

return M;
